package com.moyq5.permit.center.feign.server.controller;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.permit.center.bean.entity.Charge;
import com.moyq5.permit.center.bean.search.Charge4Search;
import com.moyq5.permit.center.core.service.ChargeService;
import com.moyq5.permit.center.feign.server.BaseController;
import com.moyq5.permit.center.feign.server.jackson.filter.ChargeFilter;


/**
 * @author Moyq5’s Tools
 * @since 2019-07-30 18:27:32
 */
@RestController
@RequestMapping("/charge")
public class ChargeController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(ChargeController.class);
	
	@Autowired
	private ChargeService chargeService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param chargeId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long chargeId, String filter) {
		log.debug(">>>>oneById: chargeId={},filter={}", chargeId, filter);
		Charge charge = chargeService.one(chargeId);
		ObjectResult<Charge> objRst = new ObjectResult<Charge>();
		objRst.setData(charge);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(ChargeFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param charge4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<Charge4Search, String> charge4s) {
		log.debug(">>>>count: ");
		Long count = chargeService.count(charge4s.getEntity(), charge4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param charge4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<Charge4Search, Sorting> charge4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		Charge charge = chargeService.one(charge4s.getEntity(), charge4s.getCustom());
		ObjectResult<Charge> objRst = new ObjectResult<Charge>();
		objRst.setData(charge);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(ChargeFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param charge4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<Charge4Search, Sorting> charge4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<Charge> list = chargeService.list(charge4s.getEntity(), charge4s.getCustom());
		ListResult<Charge> listRst = new ListResult<Charge>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(ChargeFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param charge4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<Charge4Search, Paging> charge4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<Charge> page = chargeService.page(charge4s.getEntity(), charge4s.getCustom());
		PageResult<Charge> pageRst = new PageResult<Charge>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(ChargeFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody Charge form) {
		log.debug(">>>>add: Charge");
		Charge charge = chargeService.persist(form);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(charge.getChargeId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody Charge form) {
		log.debug(">>>>update: Charge");
		Charge charge = chargeService.one(form.getChargeId());
		EntityUtils.combine(form, charge);
		chargeService.merge(charge);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param chargeId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long chargeId) {
		log.debug(">>>>delete: chargeId={}", chargeId);
		chargeService.remove(chargeId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月31日
	 * @param charge4s
	 * @return
	 */
	@PostMapping("/sumAmount")
	@ResponseBody
	public ObjectResult<BigDecimal> sumAmount(@RequestBody Body<Charge4Search, String> charge4s) {
		log.debug(">>>>sumAmount: ");
		BigDecimal sumAmount = chargeService.sumAmount(charge4s.getEntity(), charge4s.getCustom());
		ObjectResult<BigDecimal> result = new ObjectResult<BigDecimal>();
		result.setData(sumAmount);
		result.setOk();
		return result;
	}
}
